//给定一个很长的数组 (长度 1000w), 通过随机数的方式生成 1-100 之间的整数.
//实现代码, 能够创建两个线程, 对这个数组的所有元素求和.
//其中线程1 计算偶数下标元素的和, 线程2 计算奇数下标元素的和.
//最终再汇总两个和, 进行相加
//记录程序的执行时间.


import java.util.Random;

public class Demo11 {
    private static long sum1 = 0, sum2 = 0,sum = 0;
    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        int[] array = new int[10000000];
        for (int i = 0; i < array.length; i++) {
            array[i] = new Random().nextInt(100) + 1;
        }

        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < array.length; i += 2) {
                sum1 += array[i];
            }
        });

        Thread thread2 = new Thread(() -> {
            for (int i = 1; i < array.length; i += 2) {
                sum2 += array[i];
            }
        });
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();

        sum = sum1 + sum2;
        long end = System.currentTimeMillis();

        System.out.println("和："+ sum);
        System.out.println("时间："+ (end - start));
    }
}
